/*
 * @Author: xuesong
 * @Date: 2021-12-16 18:48:10
 * @LastEditors: xuesong
 * @LastEditTime: 2022-03-23 17:24:06
 * @FilePath: \BLE-SDK-SVN499-V10\projects\ble_mulit\simpleBleMultiConnection\src\mast\Display.c
 */

#include "sdk_config.h"
#include "clock.h"
#include "log.h"
#include "OSAL.h"
#include "gpio.h"
// #include "CH456IF.h"
// #include "tm1640.h"
#include "Display.h"
#include "halPeripheral.h"
// #include "multi_role.h"
#include "user_prcess.h"
//---------------------------------------------------------------------------------------------------------
const unsigned char seg[]={
         seg_A+seg_B+seg_C+seg_D+seg_E+seg_F,			// char "0"
         seg_B+seg_C,									// char "1"
         seg_A+seg_B+seg_D+seg_E+seg_G, 				// char "2"
         seg_A+seg_B+seg_C+seg_D+seg_G,      			// char "3"
         seg_B+seg_C+seg_F+seg_G,						// char "4"
         seg_A+seg_C+seg_D+seg_F+seg_G,   				// char "5"
         seg_A+seg_C+seg_D+seg_E+seg_F+seg_G,			// char "6"
         seg_A+seg_B+seg_C,								// char "7"
         seg_A+seg_B+seg_C+seg_D+seg_E+seg_F+seg_G,     // char "8"
         seg_A+seg_B+seg_C+seg_D+seg_F+seg_G,			// char "9"
         seg_A+seg_B+seg_C+seg_E+seg_F+seg_G, 			// char "A"
         seg_C+seg_D+seg_E+seg_F+seg_G,  				// char "b"
         seg_A+seg_D+seg_E+seg_F,          				// char "C"
         seg_B+seg_C+seg_D+seg_E+seg_G,       			// char "d"
         seg_A+seg_D+seg_E+seg_F+seg_G,    				// char "E"
         seg_A+seg_E+seg_F+seg_G,						// char "F"
         seg_A+seg_C+seg_D+seg_E+seg_F+seg_G,			// char "G"
         seg_B+seg_C+seg_E+seg_F+seg_G,					// char	"H"
         seg_E+seg_F,		   							// char	"I"
         seg_B+seg_C+seg_D,								// char	"J"
         0,												// char	"K"
         seg_D+seg_E+seg_F,								// char	"L"
         0, 											// char "M"
         seg_A+seg_B+seg_C+seg_E+seg_F, 				// char "N"
         seg_C+seg_D+seg_E+seg_G,						// char "o"
         seg_A+seg_B+seg_E+seg_F+seg_G, 				// char "P"
         0,												// char "Q"
         seg_E+seg_G, 							        // char "r"
         seg_A+seg_C+seg_D+seg_F+seg_G,       			// char "S"
         seg_D+seg_E+seg_F+seg_G,				   		// char	"T"
         seg_B+seg_C+seg_D+seg_E+seg_F,				    // char	"U"
         0,								 				// char	"V"
         0,												// char	"W"
         0,												// char	"x"
         0,												// char	"Y"
         0,												// char	"Z"
         seg_G,								            //char "-"
         seg_C+seg_G+seg_E, 				            //char "n"
         seg_A+seg_B+seg_G+seg_F, 				        //char "q" as up o
         0,												// ''space''
		};
//-----------------------------------------------------
Display_t   DisplayData;
//-----------------------------------------------------
void displayChar2Tuble(char *str,uint8 *tempDisplay,uint8 TubleNumber)
{
    uint8_t len = osal_strlen((char *)str);
    char *pData = str;
    if(len > TubleNumber)
    {
        len = TubleNumber;
    }
    for(uint8_t i = 0;i<len;i++)
        tempDisplay[i] = *pData++;
}
void displayNumber2Number(uint16 data,uint8 point,uint8 *tempDisplay,uint8 TubleNumber)
{
    uint8 tempDigit = 0;
    uint8 i = 0;

    if(point > (TubleNumber -1))            //0.0^X
        point = (TubleNumber -1);
    for(i = 0 ; i < TubleNumber ; i++)
        {
            tempDigit = data % 10;
            data /= 10;
            tempDisplay[TubleNumber - 1 - i] = tempDigit;  
            if(data == 0 && i >= point){
                break;
            }      
        }
    for(i++ ; i < TubleNumber ;i++ )
    {
        tempDisplay[TubleNumber - 1 - i] = ' ';         //高位留空
    }
}
void displayNumber(Display_t *TempDisplay,uint8_t value)
{
    int8_t i = 0;
    uint8_t *pData = NULL;

    pData = TempDisplay->Data;
    i = sizeof(TempDisplay->Data);
    for(;i>0;i--)
        *pData++ = value;
}

void displayAllON(Display_t *TempDisplay)
{
    int8_t i = 0;
    uint8_t *pData = NULL;
    pData = (uint8_t *)&TempDisplay->SymbleFlag.flag;
    for(i = 0; i <  sizeof(TempDisplay->SymbleFlag.flag); i++)
    {
        *pData++ = 0xFF;
    }
    displayNumber(TempDisplay,0x8);
}

void displayClrBuf(Display_t *TempDisplay)
{
    uint8_t i = 0;
    uint8_t *pData = NULL;
    TempDisplay->SymbleFlag.flag = 0;

    pData = TempDisplay->Data;
    i = sizeof(TempDisplay->Data);
    for(;i>0;i--)
        *pData++ = ' ';

}

static uint8_t GetTublerCharcterTable(const uint8 tempChar)
{
    uint8   CharNumber = 0;
    if(tempChar <= 9)
        {CharNumber = tempChar;}         // 数字 
    else if(tempChar >= '0' && tempChar <= '9')
        {CharNumber = tempChar -'0';}
    else if(tempChar >= 'A' && tempChar <= 'Z')
        {CharNumber = tempChar - 'A'+ 10;}
    else if(tempChar >= 'a' && tempChar <= 'z')
        {CharNumber = tempChar - 'a'+ 10;}

    {   //special char
        if(tempChar == '-')
            CharNumber = sizeof(seg)-4;
        else if(tempChar == 'n')
            CharNumber = sizeof(seg)-3; 
        else if(tempChar == 'q')
            CharNumber = sizeof(seg)-2;
        else if(tempChar == ' ')
            CharNumber = sizeof(seg)-1;

    }
    return seg[CharNumber];
}

void DisplayDriverInit(void)
{
    // TM1621_Init();
    // TM1628_Init();
    // Tm1640_IO_Init();
    // VK1629a_Init();
    // TM1628_Init();
	// HalGpioSet(BACKLIGHT_PIN,Bit_ENABLE);
    // HalGpioPinInit(BACKLIGHT_PIN, GPIO_OUTPUT);
    // HalGpioPupdConfig(BACKLIGHT_PIN, GPIO_PULL_UP);
    HalGpioSet(P11,Bit_ENABLE);
    HalGpioPinInit(P11, GPIO_OUTPUT);
    HalGpioPupdConfig(P11, GPIO_FLOATING);
    HalGpioSet(P14,Bit_ENABLE);
    HalGpioPinInit(P14, GPIO_OUTPUT);
    HalGpioPupdConfig(P14, GPIO_FLOATING);
}

void DisplayDriverPowerDown(void)
{
    // TM1621_Enter_Standby();
    WaitMs(2);
    // VK1629a_PowerDown();
    // Tm1628A_IO_PowerDown();
    // TM1621_Lowlevel_Init();
    // Tm1628A_IO_PowerDown();
    // Tm1640_IO_PowerDown();
	// HalGpioSet(BACKLIGHT_PIN,Bit_DISABLE);
    HalGpioSet(P11,Bit_ENABLE);
    HalGpioSet(P14,Bit_ENABLE);
}

void Display2Driver(Display_t TempDisplayData)
{
    DisplayBufferType   TempDisplay_Buff[DISPLAY_BUFF_SIZE];
    static DisplayBufferType oldDisplayBuff[DISPLAY_BUFF_SIZE];
    static uint8_t SameCnt = 0;

    uint8   i = 0;
    uint8   tempTublerNumber = 0;//sizeof(TempDisplayData.numberData);

    osal_memset(TempDisplay_Buff,0,sizeof(TempDisplay_Buff));


    tempTublerNumber = sizeof(TempDisplayData.Data);
    for( i = 0 ;i < tempTublerNumber ; i++)
    {
        TempDisplay_Buff[i] = GetTublerCharcterTable(TempDisplayData.Data[i]);
    }


    if(osal_memcmp(oldDisplayBuff,TempDisplay_Buff,sizeof(TempDisplay_Buff)))
    {
        SameCnt ++;
        if(SameCnt > 6)
            SameCnt = 0;
    }else{
        SameCnt = 0;
    }
    osal_memcpy(oldDisplayBuff,TempDisplay_Buff,sizeof(TempDisplay_Buff));
    if(!SameCnt)
    {
        Data2TM1628(TempDisplay_Buff,sizeof(TempDisplay_Buff));
        // LOG("display buff send:");
        // my_dump_byte(TempDisplay_Buff,sizeof(TempDisplay_Buff));
        // LOG("\r\n");
    }
}


void HalDisplayEvtProcss(void)
{
    Display2Driver(DisplayData);
    if(DisplayData.SymbleFlag.bit.green)
        HalGpioSet(P14,Bit_DISABLE);
    else
        HalGpioSet(P14,Bit_ENABLE);
    if(DisplayData.SymbleFlag.bit.red)
        HalGpioSet(P11,Bit_DISABLE);
    else
        HalGpioSet(P11,Bit_ENABLE);
}
